<template>
  <div class="login-container">
    <van-nav-bar title="登录" />
    
    <div class="login-form">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="username"
            name="username"
            label="用户名"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            label="密码"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
        </van-cell-group>
        
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit">
            登录
          </van-button>
        </div>
        
        <div class="register-link">
          <router-link to="/register">还没有账号？立即注册</router-link>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { showToast } from 'vant'

const router = useRouter()
const userStore = useUserStore()

const username = ref('')
const password = ref('')

const onSubmit = () => {
  // 模拟登录验证
  if (username.value === 'admin' && password.value === '123456') {
    userStore.setToken('mock-token')
    userStore.setUserInfo({
      username: username.value,
      nickname: '测试用户'
    })
    showToast('登录成功')
    router.push('/home')
  } else {
    showToast('用户名或密码错误')
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background-color: #f7f8fa;
}

.login-form {
  padding: 20px;
}

.register-link {
  text-align: center;
  margin-top: 16px;
}

.register-link a {
  color: #4794eb;
  text-decoration: none;
}
</style> 